package com.sanpu.kxe.business.main.presenter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.contract.NewsContentContract;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.shareutil.ShareUtil;
import com.sanpu.kxe.jump.shareutil.share.ShareListener;
import com.sanpu.kxe.jump.shareutil.share.SharePlatform;
import com.sanpu.kxe.utils.ToastUtil;

import rx.subscriptions.CompositeSubscription;

/**
 * Description：新闻详情页
 * Created by 薛勇军 on 2019/3/21.
 */

public class NewsContentPresenter implements NewsContentContract.Presenter {

    private NewsContentContract.View mView;

    private CompositeSubscription mSubscription;

    private String mShareUrl, mShareTitle, mShareContent, mSharePicture;

    public NewsContentPresenter(NewsContentContract.View mView,
                                String shareUrl, String shareTitle, String shareContent, String sharePicture) {
        this.mView = mView;
        this.mShareUrl = shareUrl;
        this.mShareTitle = shareTitle;
        this.mShareContent = shareContent;
        this.mSharePicture = sharePicture;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void prepareMedia(final @SharePlatform.Platform int module) {
        if (TextUtils.isEmpty(mShareUrl)) return;
        ShareListener shareListener = new ShareListener() {
            @Override
            public void shareSuccess() {
//                String shareAppType = "";
//                switch (module) {
//                    case SharePlatform.WX:
//                        shareAppType = CommonConfig.SHARETYPE_WX;
//                        break;
//                    case SharePlatform.WX_TIMELINE:
//                        shareAppType = CommonConfig.SHARETYPE_WXCIRCLE;
//                        break;
//                    case SharePlatform.QQ:
//                        shareAppType = CommonConfig.SHARETYPE_QQ;
//                        break;
//                    case SharePlatform.QZONE:
//                        shareAppType = CommonConfig.SHARETYPE_QZONE;
//                        break;
//                }
//                ToastUtil.showTextViewPrompt("分享成功");
//                if (!TextUtils.isEmpty(shareAppType)) {
//                    Subscription subscription = MainModel.getInstance().sharingIntegral(shareAppType)
//                            .subscribeOn(Schedulers.io())
//                            .unsubscribeOn(Schedulers.io())
//                            .observeOn(AndroidSchedulers.mainThread())
//                            .subscribe(new Observer<String>() {
//                                @Override
//                                public void onCompleted() {
//
//                                }
//
//                                @Override
//                                public void onError(Throwable e) {
//
//                                }
//
//                                @Override
//                                public void onNext(String s) {
//
//                                }
//                            });
//                    mSubscription.add(subscription);
//                }
            }

            @Override
            public void shareFailure(Exception e) {
                ToastUtil.showTextViewPrompt(e.getMessage());
            }

            @Override
            public void shareCancel() {
            }
        };
        try {
            if (TextUtils.isEmpty(mSharePicture)) {
                Bitmap bitmap = BitmapFactory.decodeResource(mView.getContext().getResources(), R.drawable.ic_logo);
                ShareUtil.shareMedia(mView.getContext(), module, mShareTitle, mShareContent, mShareUrl, bitmap, shareListener);
            } else {
                ShareUtil.shareMedia(mView.getContext(), module, mShareTitle, mShareContent, mShareUrl,
                        UrlConstants.getBaseUrl()+mSharePicture, shareListener);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
